/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.yi.yier.boot.autoconfigure.codegenerator.api.controller;

import com.yi.yier.boot.autoconfigure.codegenerator.api.base.condition.BaseCondition;
import com.yi.yier.boot.autoconfigure.codegenerator.api.base.interfaces.ICondition;
import com.yi.yier.boot.autoconfigure.codegenerator.api.base.interfaces.IModelRestApi;
import com.yi.yier.boot.autoconfigure.codegenerator.api.base.response.ResponseJson;
import com.yi.yier.boot.autoconfigure.codegenerator.api.exception.BusinessException;
import com.yi.yier.boot.autoconfigure.codegenerator.api.exception.code.ApiErrorCode;
import com.yi.yier.boot.autoconfigure.codegenerator.api.service.mybatis.BaseMybatisService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-06-29 20:54
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-06-29
 * @modify reason: {方法名}:{原因}
 * ...
 */
public abstract class BaseWebController<T> implements IModelRestApi<T, BaseCondition> {

    public BaseMybatisService baseMybatisService;

    protected abstract BaseMybatisService getBaseMybatisService();


    @RequestMapping(value="addition", method = RequestMethod.POST)
    @Override
    public ResponseJson<T> addition(@RequestBody T record) {
        getBaseMybatisService().insert(record);
        return new ResponseJson();
    }


    @RequestMapping(value="deletion", method = RequestMethod.GET)
    @Override
    public ResponseJson deletion(@RequestParam Integer id) {
        getBaseMybatisService().deleteByPrimaryKey(id);
        return new ResponseJson();
    }

    @RequestMapping(value="update", method = RequestMethod.POST)
    @Override
    public ResponseJson<T> update(@RequestBody T record) {
        getBaseMybatisService().updateByPrimaryKey(record);
        return new ResponseJson<T>().ok();
    }

    @RequestMapping(value="select", method = RequestMethod.POST)
    @Override
    public ResponseJson<List<T>> select(BaseCondition condition) {
        throw new BusinessException(ApiErrorCode.FAILED.getCode());
    }

}